﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace SalomonWebsite
{
    public partial class ProSeries : System.Web.UI.Page
    {

        public string strCon = Global.Connection;
        public string ImageWeb = Global.ImageWeb;
        public int ParPKID;
        public int PKID;
        public string Val = "";

        public string catName = "";
        public string description = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request["parid"] != null && Request["parid"] != "" && Request["pkid"] != null && Request["pkid"] != "" && Request["val"] != null && Request["val"] != "")
                {
                    ParPKID = Convert.ToInt32(Request["parid"]);
                    PKID = Convert.ToInt32(Request["pkid"]);
                    Val = Request["val"].ToString();

                    DataTable dtCat = new DataTable();
                    dtCat = Bll.Category.Single(strCon, PKID);
                    if (dtCat.Rows.Count == 1)
                    {
                        DataRow[] drs = dtCat.Select(" [CatCode] = '" + Val + "'");
                        if (drs.Length == 1)
                        {
                            catName = drs[0]["CatName"].ToString();
                            description = drs[0]["Description"].ToString();

                            this.PageNote.PKID = PKID;
                            this.PageNote.ParPKID = ParPKID;
                            this.CatAd.ParPKID = ParPKID;

                            this.CatTree.PKID = PKID;
                            this.CatTree.ParPKID = ParPKID;
                            this.CatTree.FarPKID = Convert.ToInt32(drs[0]["ParID"]);

                            GetSeriesList(strCon, PKID);
                        }
                        else
                        {
                            Response.Redirect("FileNotFound.aspx");
                        }
                    }
                    else
                    {
                        Response.Redirect("FileNotFound.aspx");
                    }
                }
                else
                {
                    Response.Redirect("FileNotFound.aspx");
                }
            }
        }


        //Find Salomon on
        //protected void GetCatList(int parID)
        //{
        //    DataTable dt = Bll.Category.MULTIPLE(strCon, parID);
        //    if (dt != null && dt.Rows.Count > 0)
        //    {
        //        this.rpCategory.DataSource = dt.DefaultView;
        //        this.rpCategory.DataBind();
        //        dt.Dispose();
        //    }
        //}




        #region

        //protected string strCon = Global.Connection;
        //public string ImageWeb = Global.ImageWeb;
        //protected int PKID = 0;
        //protected int parId = 0;
        //protected string parCatName = "";
        //public int catParId = 0;  
        //public string catName = "";
        //public int catRank = 0;
        //public string description = "";
        //public string rightAdTitle = "";
        //public string rightAdImgURL = "";
        //public string rightAdDescription = "";
        //public string rightAdContent = "";

        //public string nowCatName = "";

        //protected void Page_Load(object sender, EventArgs e)
        //{
        //    if(!IsPostBack)
        //    {
        //        PKID = Convert.ToInt32(Request["pkid"]);
        //        GetNowCat(strCon, PKID);
        //        DataTable dt = Bll.Category.Single(strCon, PKID);
        //        if (dt != null && dt.Rows.Count > 0) 
        //        {
        //            parId = Convert.ToInt32(dt.Rows[0]["ParID"].ToString());
        //            parCatName = dt.Rows[0]["ParName"].ToString();
        //        }
        //        GetSeriesList(strCon, PKID);
        //        GetLeftTemp(strCon, 1);
        //        GetPrvCatByPKID(strCon, parId);
        //        GetLeft(strCon);
        //        GetRightAd(strCon,parId);
        //    }
        //}

        //protected void GetNowCat(string strCon,int pkid) 
        //{
        //    DataTable dt = Bll.Category.Single(strCon, pkid);
        //    if (dt != null && dt.Rows.Count > 0) {
        //        nowCatName = dt.Rows[0]["CatName"].ToString();
        //    }
        //}

        //绑定系列
        protected void GetSeriesList(string strCon, int id)
        {
            DataTable dtSerList = new DataTable();
            dtSerList = Bll.Series.MULTIPLE_Pro(strCon, id);
            if (dtSerList != null && dtSerList.Rows.Count > 0)
            {
                this.repSeries.DataSource = dtSerList.DefaultView;
                this.repSeries.DataBind();
                dtSerList.Dispose();
            }
        }
        //绑定系列对应的产品
        protected void repSeries_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Repeater repProduct = (Repeater)e.Item.FindControl("repPro");
                //找到分类repeater关联的数据项 
                DataRowView drv = (DataRowView)e.Item.DataItem;
                //提取分类id 
                int serId = Convert.ToInt32(drv["PKID"]);
                //根据分类id查询该分类下的产品，并绑定产品repeater 
                repProduct.DataSource = GetProductListBySerId(strCon, serId);
                repProduct.DataBind();
                repProduct.Dispose();
            }
        }

        protected DataTable GetProductListBySerId(string strCon, int serId)
        {
            return Bll.Product.MULTIPLE(strCon, serId);
        }


        //protected DataTable GetLeftTemp(string strCon, int id)
        //{
        //    DataTable dtLeft = Bll.Category.MULTIPLE(strCon, id);    //二级
        //    return dtLeft;
        //}

        //protected void GetPrvCatByPKID(string strCon, int PKID)
        //{
        //    DataTable dt = Bll.Category.Single(strCon, PKID);
        //    if (dt != null && dt.Rows.Count > 0)
        //    {
        //        catName = dt.Rows[0]["CatName"].ToString();
        //        catParId = Convert.ToInt32(dt.Rows[0]["ParID"].ToString());
        //        catRank = Convert.ToInt32(dt.Rows[0]["CatRank"].ToString());
        //        description = dt.Rows[0]["Description"].ToString();
        //    } 
        //}

        //protected DataTable CreateTableLeft()
        //{
        //    DataTable dt = new DataTable();
        //    dt.Columns.Add("PKID", typeof(Int32));
        //    dt.Columns.Add("CatName", typeof(string));
        //    dt.Columns.Add("ParID", typeof(Int32));
        //    dt.Columns.Add("CatRank", typeof(Int32));
        //    dt.Columns.Add("Description", typeof(string));
        //    return dt;
        //}

        //protected void GetLeft(string strCon)
        //{
        //    int index = 0;
        //    DataTable dt = GetLeftTemp(strCon, 1);
        //    for (int i = 0; i < dt.Rows.Count; i++)
        //    {
        //        if (Convert.ToInt32(dt.Rows[i]["PKID"].ToString()) == parId)
        //        {
        //            index = i;
        //            break;
        //        }
        //    }
        //    dt.Rows.Remove(dt.Rows[index]);
        //    DataTable dtLeft = CreateTableLeft();
        //    if (dt != null && dt.Rows.Count > 0)
        //    {
        //        for (int i = 0; i <= dt.Rows.Count; i++)
        //        {
        //            DataRow newRow = dtLeft.NewRow();
        //            if (i == 0)
        //            {
        //                newRow["PKID"] = Convert.ToInt32(dt.Rows[i]["PKID"].ToString());
        //                newRow["CatName"] = dt.Rows[i]["CatName"].ToString();
        //                newRow["ParID"] = Convert.ToInt32(dt.Rows[i]["ParID"].ToString());
        //                newRow["CatRank"] = Convert.ToInt32(dt.Rows[i]["CatRank"].ToString());
        //                newRow["Description"] = dt.Rows[i]["Description"].ToString();
        //            }
        //            else if (i == 1)
        //            {
        //                newRow["PKID"] = parId;
        //                newRow["CatName"] = catName;
        //                newRow["ParID"] = parId;
        //                newRow["CatRank"] = catRank;
        //                newRow["Description"] = description;
        //            }
        //            else if (i >= 2)
        //            {
        //                newRow["PKID"] = Convert.ToInt32(dt.Rows[i - 1]["PKID"].ToString());
        //                newRow["CatName"] = dt.Rows[i - 1]["CatName"].ToString();
        //                newRow["ParID"] = Convert.ToInt32(dt.Rows[i - 1]["ParID"].ToString());
        //                newRow["CatRank"] = Convert.ToInt32(dt.Rows[i - 1]["CatRank"].ToString());
        //                newRow["Description"] = dt.Rows[i - 1]["Description"].ToString();
        //            }
        //            dtLeft.Rows.Add(newRow);
        //        }
        //    }
        //    this.repLeft.DataSource = dtLeft.DefaultView;
        //    this.repLeft.DataBind();
        //    repLeft.Dispose();
        //}

        ////二级分类
        //protected void repLeft_ItemDataBound(object sender, RepeaterItemEventArgs e)
        //{
        //    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        //    {
        //        Repeater rep = (Repeater)e.Item.FindControl("repLeftList");
        //        //找到分类repeater关联的数据项 
        //        DataRowView drv = (DataRowView)e.Item.DataItem;
        //        //提取分类id 
        //        int serId = Convert.ToInt32(drv["PKID"]);
        //        //根据分类id查询该分类下的产品，并绑定产品repeater 
        //        rep.DataSource = GetLeftTemp(strCon, serId);
        //        rep.DataBind();
        //    }
        //}

        //public void GetRightAd(string strCon, int parId)
        //{
        //    int prvParId = 0;
        //    DataTable dtPrv = Bll.Category.Single(strCon, parId);
        //    if (dtPrv != null && dtPrv.Rows.Count > 0)
        //    {
        //        prvParId = Convert.ToInt32(dtPrv.Rows[0]["ParID"].ToString());
        //    }
        //    DataTable dt = Bll.CategoryAd.Single(strCon, prvParId);
        //    if (dt != null && dt.Rows.Count > 0)
        //    {
        //        rightAdTitle = dt.Rows[0]["AdTitle"].ToString();
        //        rightAdImgURL = dt.Rows[0]["ImgURL"].ToString();
        //        rightAdDescription = dt.Rows[0]["Description"].ToString();
        //        rightAdContent = dt.Rows[0]["AdContent"].ToString();
        //    }
        //}


        #endregion

    }
}
